import {defineStore} from "pinia";
import {reactive, ref} from "vue";
import {reqCode, reqLogin} from "@/api/user";
import {loginData} from "@/api/user/type.ts";


// 用户登录
export const  useLogin=defineStore('Login',()=>{
    // login显示标志
    let dialogFormVisible=ref<boolean>(false)
    // 验证码
    let code=ref<string>('')
    // 用户消息
    let userInfo=ref({
    })


    // 获取验证码
    const getCode=async (phone:string)=>{
        let res=await reqCode(phone)
        if(res.code===200){
            code.value=res.data
        }
        else {
            alert('请求失败')
        }

    }
    // 用户登录
    const postLogin=async (data:loginData)=>{
        let res=await reqLogin(data)
        if(res.code===200){
            userInfo.value=res.data
        }
        else {
            return Promise.reject(new Error(res.message))


        }

    }

    return{
        dialogFormVisible,
        code,
        userInfo,
        getCode,
        postLogin
    }

},{
    persist: {
        enabled: true, //Store中数据持久化生效
        strategies: [
            // storage 可选localStorage或sessionStorage
            // paths 给指定数据持久化
            { key: 'userInfo', storage: localStorage, paths: ['userInfo'] }
        ]
    },
})

